package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallQuan;
import org.linlinjava.litemall.db.domain.LitemallType;
import org.linlinjava.litemall.db.domain.LitemallUserEr;
import org.linlinjava.litemall.db.domain.LitemallUserGoods;
import org.linlinjava.litemall.db.service.LitemallQuanService;
import org.linlinjava.litemall.db.service.LitemallTypeService;
import org.linlinjava.litemall.db.service.LitemallUserErService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.db.domain.LitemallQuan.Column.deleted;
import static org.linlinjava.litemall.db.domain.LitemallQuan.Column.pid;


@RestController
@RequestMapping("/wx/quan")
@Validated
public class WxQuanController {
    private final Log logger = LogFactory.getLog(WxQuanController.class);

    @Autowired
    private LitemallQuanService quanService;

    @Autowired
    private LitemallUserErService userEr;

    @Autowired
    private LitemallTypeService typeService;


    @PostMapping("selectTypeQuan")
    public Object selectTypeQuan(@RequestBody LitemallQuan litemallQuan, HttpServletRequest request) {
        Integer pid = litemallQuan.getPid();
        if (pid == null) {
            return ResponseUtil.badArgument();
        }
        List<LitemallQuan> quanList = quanService.queryAll(pid);
        List<LitemallQuan> list = new ArrayList<>();
        for (LitemallQuan quan : quanList) {
            if ("0".equals(quan.getDeleted())) {
                list.add(quan);
            }
        }
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("quanList", list);
        return ResponseUtil.ok(result);
    }

    @PostMapping("selectQuan")
    public Object selectQuan(@RequestBody LitemallQuan litemallQuan, HttpServletRequest request) {
        Integer pid = litemallQuan.getPid();
        if (pid == null) {
            return ResponseUtil.badArgument();
        }
        List<LitemallQuan> quanList = quanService.queryAll(pid);
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("quanList", quanList);
        return ResponseUtil.ok(result);
    }

    @PostMapping("toppingQuan")
    public Object toppingQuan() {
        List<LitemallQuan> quanList = quanService.queryAllQuan();
        List<LitemallQuan> quan = new ArrayList<>();
        Map<Object, Object> result = new HashMap<Object, Object>();
        for (LitemallQuan litemallQuan : quanList) {
            String pid = String.valueOf(litemallQuan.getPid());
            LitemallType litemallType = typeService.selectType(pid);
            if (litemallType != null) {
                if (litemallQuan.getDesc() == null || "".equals(litemallQuan.getDesc())) {
                    quan.add(litemallQuan);
                }
            }
        }
        result.put("quanList", quan);
        return ResponseUtil.ok(result);
    }

    @PostMapping("selectAllQuan")
    public Object selectAllQuan() {
        List<LitemallQuan> quanList = quanService.queryAllQuan();
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("quanList", quanList);
        return ResponseUtil.ok(result);
    }

    @PostMapping("updateStat")
    public Object updateStat(@RequestBody LitemallQuan litemallQuan) {
        quanService.update(litemallQuan);
        return ResponseUtil.ok();
    }

    @PostMapping("duihuan")
    public Object update(@RequestBody LitemallQuan litemallQuan) {
        Integer id = litemallQuan.getId();
        BigDecimal price1 = litemallQuan.getPrice();
        String balance = String.valueOf(price1);
        String username = litemallQuan.getDesc();

        LitemallQuan selectQuan = quanService.selectById(id);

        if ("1".equals(selectQuan.getDeleted())){
            return ResponseUtil.fail(1001, "此券已被兑换");
        }
        List<LitemallUserEr> litemallUserErs = userEr.queryByUsernameEr(username);
        LitemallUserEr litemallUserEr = litemallUserErs.get(0);
        int i = Integer.parseInt(balance);
        int i1 = Integer.parseInt(litemallUserEr.getBalance());
        if (i1 < i) {
            return ResponseUtil.fail(1001, "余额不足");
        }
        if (0 == id) {
            return ResponseUtil.badArgument();
        }
        litemallQuan.setDeleted("1");
        litemallQuan.setId(id);
        quanService.update(litemallQuan);

        userEr.updateUserErById(username, balance);
        List<LitemallUserEr> litemallUserErs1 = userEr.queryByUsernameEr(username);
        return ResponseUtil.ok(litemallUserErs1);

    }

    @PostMapping("selectMyQuan")
    public Object selectMyQuan(@RequestBody LitemallQuan litemallQuan, HttpServletRequest request) {
        String desc = litemallQuan.getDesc();
        if (desc == null) {
            return ResponseUtil.badArgument();
        }
        List<LitemallQuan> quanList = quanService.selectByDesc(desc);
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("quanList", quanList);
        return ResponseUtil.ok(result);
    }
}
